package contract;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.PagingVo;
import counsel.CounselDTO;
import code.CodeDAO;
import code.CodeDTO;

public class ContractList extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		req.setCharacterEncoding("utf-8");

		String full = req.getParameter("full");
		String l_full = (String) req.getSession().getAttribute("l_full");

		ContractDTO cont = new ContractDTO();
		ContractDAO dao = new ContractDAO();
		// 검색 했던적있으면 여기에 걸림
		if ((full != null && full.equals("full")) || l_full != null) {
			cont.setFull("full");
			dao.setCont(cont);
			if (full != null && full.equals("full")) {// 검색버튼 눌렀을시
				req.getSession().setAttribute("l_full", "full");
				cont.setSa_search(req.getParameter("sa_search"));
				req.getSession().setAttribute("l_sa_search", cont.getSa_search());
				cont.setPd_search(req.getParameter("pd_search"));
				req.getSession().setAttribute("l_pd_search", cont.getPd_search());
				cont.setCt_search(req.getParameter("ct_search"));
				req.getSession().setAttribute("l_ct_search", cont.getCt_search());
				cont.setSa_dpm(req.getParameter("sa_dpm"));
				req.getSession().setAttribute("l_sa_dpm", cont.getSa_dpm());
				cont.setSa_gd(req.getParameter("sa_gd"));
				req.getSession().setAttribute("l_sa_gd", cont.getSa_gd());
				cont.setCs_loan(req.getParameter("cs_loan"));
				req.getSession().setAttribute("l_cs_loan", cont.getCs_loan());
				cont.setCt_ing(req.getParameter("ct_ing"));
				req.getSession().setAttribute("l_ct_ing", cont.getCt_ing());
			}else if (l_full != null && l_full.equals("full")) {// 페이징이나 검색후
				cont.setSa_search((String) req.getSession().getAttribute("l_sa_search"));
				cont.setPd_search((String) req.getSession().getAttribute("l_pd_search"));
				cont.setCt_search((String) req.getSession().getAttribute("l_ct_search"));
				cont.setSa_dpm((String) req.getSession().getAttribute("l_sa_dpm"));
				cont.setSa_gd((String) req.getSession().getAttribute("l_sa_gd"));
				cont.setCs_loan((String) req.getSession().getAttribute("l_cs_loan"));
				cont.setCt_ing((String)req.getSession().getAttribute("l_ct_ing"));
			}
			// 사원관련 검색
			if (cont.getSa_search().equals("sa_nm")) {
				if (req.getParameter("sa_find") != null) {
					cont.setSa_nm(req.getParameter("sa_find"));
					req.getSession().setAttribute("l_sa_nm", cont.getSa_nm());
				} else {
					cont.setSa_nm((String) req.getSession().getAttribute("l_sa_nm"));
				}
				cont.setSa_no("");
				req.getSession().setAttribute("l_sa_no", "");
			} else if (cont.getSa_search().equals("sa_no")) {
				if (req.getParameter("sa_find") != null) {
					cont.setSa_no(req.getParameter("sa_find"));
					req.getSession().setAttribute("l_sa_no", cont.getSa_no());
				} else {
					cont.setSa_no((String) req.getSession().getAttribute("l_sa_no"));
				}
				cont.setSa_nm("");
				req.getSession().setAttribute("l_sa_nm", "");
			}
			// 상품관련 검색
			if (cont.getPd_search().equals("pd_nm")) {
				if (req.getParameter("pd_find") != null) {
					cont.setPd_nm(req.getParameter("pd_find"));
					req.getSession().setAttribute("l_pd_nm", cont.getPd_nm());
				} else {
					cont.setPd_nm((String) req.getSession().getAttribute("l_pd_nm"));
				}
				cont.setPd_no("");
				req.getSession().setAttribute("l_pd_no", "");
			} else if (cont.getPd_search().equals("pd_no")) {
				if (req.getParameter("pd_find") != null) {
					cont.setPd_no(req.getParameter("pd_find"));
					req.getSession().setAttribute("l_pd_no", cont.getPd_no());
				} else {
					cont.setPd_no((String) req.getSession().getAttribute("l_pd_no"));
				}
				cont.setPd_nm("");
				req.getSession().setAttribute("l_pd_nm", "");
			}
			// 계약관련 검색
			if (cont.getCt_search().equals("ct_no")) {
				if (req.getParameter("ct_find") != null) {
					cont.setCt_no(req.getParameter("ct_find"));
					req.getSession().setAttribute("l_ct_no", cont.getCt_no());
				} else {
					cont.setCt_no((String) req.getSession().getAttribute("l_ct_no"));
				}
				cont.setCs_no("");
				cont.setRg_pp("");
				req.getSession().setAttribute("l_cs_no", "");
				req.getSession().setAttribute("l_rg_pp", "");
			} else if (cont.getCt_search().equals("cs_no")) {
				if (req.getParameter("ct_find") != null) {
					cont.setCs_no(req.getParameter("ct_find"));
					req.getSession().setAttribute("l_cs_no", cont.getCs_no());
				} else {
					cont.setCs_no((String) req.getSession().getAttribute("l_cs_no"));
				}
				cont.setCt_no("");
				cont.setRg_pp("");
				req.getSession().setAttribute("l_ct_no", "");
				req.getSession().setAttribute("l_rg_pp", "");
			} else if (cont.getCt_search().equals("rg_pp")) {
				if (req.getParameter("ct_find") != null) {
					cont.setRg_pp(req.getParameter("ct_find"));
					req.getSession().setAttribute("l_rg_pp", cont.getRg_pp());
				} else {
					cont.setRg_pp((String) req.getSession().getAttribute("l_rg_pp"));
				}
				cont.setCt_no("");
				cont.setCs_no("");
				req.getSession().setAttribute("l_ct_no", "");
				req.getSession().setAttribute("l_cs_no", "");
			}

			// 상담일관련 검색
			if (req.getParameter("cs_sd") == null || req.getParameter("cs_sd").equals("")) {
				if (req.getSession().getAttribute("l_cs_sd") != null 
						&& !((String) req.getSession().getAttribute("l_cs_sd")).equals("")) {
					cont.setCs_sd((String) req.getSession().getAttribute("l_cs_sd"));
					cont.setCs_ed((String) req.getSession().getAttribute("l_cs_ed"));
				} else {
					cont.setCs_sd("0");
					cont.setCs_ed("0");
					req.getSession().setAttribute("l_cs_sd", "");
					req.getSession().setAttribute("l_cs_ed", "");
				}
			} else {
				cont.setCs_sd(req.getParameter("cs_sd"));
				cont.setCs_ed(req.getParameter("cs_ed"));
				req.getSession().setAttribute("l_cs_sd", cont.getCs_sd());
				req.getSession().setAttribute("l_cs_ed", cont.getCs_ed());
			}

			// 페이징 관련
			int tot = dao.totalRow();
			int nowPage = 1;
			if (req.getParameter("listPage") == null) {
				if (req.getSession().getAttribute("ctPage") != null) {
					nowPage = (int) req.getSession().getAttribute("ctPage");
				} else {
					nowPage = 1;
					req.getSession().setAttribute("ctPage", 1);
				}
			} else {
				nowPage = Integer.parseInt(req.getParameter("listPage"));
				req.getSession().setAttribute("ctPage", nowPage);
			}

			PagingVo paging = new PagingVo();
			paging = paging.computer(tot, nowPage);// 페이징 결과값 반환

			// System.out.println(full+"/"+cont.getSa_search()+"/"+cont.getSa_no()+"/"+paging.getStartNo()+"/"+paging.getEndNo()+"/"+tot);
			// 검색용 목록
			ArrayList<ContractDTO> lists = dao.lists(paging.getStartNo(),paging.getEndNo());
			//System.out.println(lists.size());
			if(lists.size() == 0){
				dao = new ContractDAO();
				dao.setCont(cont);
				nowPage = 1;
				req.getSession().setAttribute("ctPage", 1);
				paging = paging.computer(tot, nowPage);
				lists = dao.lists(paging.getStartNo(),paging.getEndNo());
			}
			
			req.setAttribute("paging", paging);// 페이징결과값 저장 저장
			req.setAttribute("lists", lists);

		} else if (full != null) {

			cont.setFull(full);
			dao.setCont(cont);

			// 페이징 관련
			int tot = dao.totalRow();
			int nowPage = 1;
			if (req.getParameter("listPage") == null) {
				if (req.getSession().getAttribute("ctPage") != null) {
					nowPage = (int) req.getSession().getAttribute("ctPage");
				} else {
					nowPage = 1;
					req.getSession().setAttribute("ctPage", 1);
				}
			} else {
				nowPage = Integer.parseInt(req.getParameter("listPage"));
				req.getSession().setAttribute("ctPage", nowPage);
			}

			PagingVo paging = new PagingVo();
			paging = paging.computer(tot, nowPage);// 페이징 결과값 반환

			req.setAttribute("paging", paging);// 페이징결과값 저장 저장
			ArrayList<ContractDTO> lists = dao.lists(paging.getStartNo(),
					paging.getEndNo());
			req.setAttribute("lists", lists);
		}
		
		ArrayList<ContractDTO> l = (ArrayList<ContractDTO>)req.getAttribute("lists");
		// 공통코드 관련
		CodeDTO code = new CodeDTO();
		CodeDAO cdao = new CodeDAO();
		// 계약상태
		code.setCd_grp1("CTING");
		cdao.setDto(code);
		ArrayList<CodeDTO> codes = cdao.returnCode();
		req.setAttribute("ctings", codes);
		// 직급
		cdao = new CodeDAO();
		code.setCd_grp1("PST");
		cdao.setDto(code);
		codes = cdao.returnCode();
		req.setAttribute("psts", codes);
		// 부서
		cdao = new CodeDAO();
		code.setCd_grp1("SADP");
		cdao.setDto(code);
		codes = cdao.returnCode();
		req.setAttribute("sadps", codes);

		if (full != null) {
			//System.out.println("검색관련");
			req.getRequestDispatcher("/intranet/contract/contract_list_view.jsp").forward(req,resp);
		} else {
			//System.out.println("다시 시작!"+req.getSession().getAttribute("l_ct_search")+"/"+cont.getCt_search());
			req.getRequestDispatcher("/intranet/index.jsp?inc_content=./contract/contract_list.jsp").forward(req, resp);
		}

	}

}
